from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, BigInteger
from sqlalchemy.orm import relationship
from datetime import datetime

from app.db.base_class import Base

class QuestionSet(Base):
    __tablename__ = "question_set"

    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True)
    title = Column(String(255), nullable=False, comment="题集标题")
    description = Column(Text, comment="题集简介或说明")
    created_at = Column(DateTime, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment="最后更新时间")

    # 建立与题目的多对多关系
    questions = relationship("Question", secondary="question_set_question", back_populates="question_sets")

class QuestionSetQuestion(Base):
    __tablename__ = "question_set_question"

    id = Column(BigInteger, primary_key=True, index=True, autoincrement=True)
    question_set_id = Column(BigInteger, ForeignKey("question_set.id", ondelete="CASCADE"), nullable=False)
    question_id = Column(BigInteger, ForeignKey("questions.id", ondelete="CASCADE"), nullable=False)
    sort_order = Column(Integer, default=0, comment="题目在题集中的排序位置") 